package com.ruoyi.framework.web.service;


import com.ruoyi.common.constant.*;
import com.ruoyi.common.core.domain.GTPush;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.apache.http.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 设备通知栏消息服务类
 */
@Component
public class DeviceNoticeMessageService {

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);


    @Autowired
    private RedisCache redisUtil;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IAndroidDeviceService androidDeviceService;

    @Autowired
    private IYxUserAndDeviceService yxUserAndDeviceService;

    @Autowired
    private IYxDeviceAndGetuiService yxDeviceAndGetuiService;

    @Autowired
    private IYxAndroidApplicationService yxAndroidApplicationService;

    @Autowired
    private IYxDeviceNoticeMessageService yxDeviceNoticeMessageService;

    public R<String> uploadNoticeDeviceMessageList(List<YxDeviceNoticeMessageVo> list){

        if(list == null || list.size() == 0 ){
            return R.fail("上传列表 不可为空");
        }

        //获取当前登录用户
        SysUser user = SecurityUtils.getLoginUser().getUser();

        if(user == null || user.getAccountType() != LoginConstants.USER_TYPE_DEVICE_STU){
            return R.fail("设备信息异常");
        }

        // 切换到后台执行服务
        AsyncManager.me().execute(
                new TimerTask()
                {
                    @Override
                    public void run()
                    {
                        for(YxDeviceNoticeMessageVo yx : list){
                            YxAndroidApplication yxAndroidApplication = yxAndroidApplicationService.selectYxAndroidApplicationByPackageName(yx.getPackageName());
                            if(yxAndroidApplication == null){
                                 continue;
                            }

                            YxDeviceNoticeMessage deviceNoticeMessage = new YxDeviceNoticeMessage();
                            deviceNoticeMessage.setAppId(yxAndroidApplication.getId());
                            deviceNoticeMessage.setContent(yx.getContent());
                            deviceNoticeMessage.setDeviceId(user.getUserName());
                            deviceNoticeMessage.setCreateTime(new Date(yx.getTimeStamp()));
                            yxDeviceNoticeMessageService.insertYxDeviceNoticeMessage(deviceNoticeMessage);
                        }

                    }
                }
        );


        return R.ok();
    }

    public R<String> uploadNoticeDeviceMessage(String packageName,String content){

        if(TextUtils.isEmpty(packageName)){
            return R.fail("packageName 不可为空");
        }

        if(TextUtils.isEmpty(content)){
            return R.fail("content 不可为空");
        }
        //获取当前登录用户
        SysUser user = SecurityUtils.getLoginUser().getUser();

        if(user == null || user.getAccountType() != LoginConstants.USER_TYPE_DEVICE_STU){
            return R.fail("设备信息异常");
        }


        YxAndroidApplication yxAndroidApplication = yxAndroidApplicationService.selectYxAndroidApplicationByPackageName(packageName);
        if(yxAndroidApplication == null){
            return R.fail("packageName 应用不存在，请先上传应用信息");
        }
        log.debug( "uploadDeviceMessage  userName = " + user.getUserName() + " , packageName = "+packageName + " , content = " + content);

        YxDeviceNoticeMessage deviceNoticeMessage = new YxDeviceNoticeMessage();
        deviceNoticeMessage.setAppId(yxAndroidApplication.getId());
        deviceNoticeMessage.setContent(content);
        deviceNoticeMessage.setDeviceId(user.getUserName());
        deviceNoticeMessage.setCreateTime(new Date());
        yxDeviceNoticeMessageService.insertYxDeviceNoticeMessage(deviceNoticeMessage);

        return R.ok();
    }


    public List<YxDeviceNoticeMessageVo> deviceNoticeMessageList() {

        //获取当前登录用户
        SysUser user = SecurityUtils.getLoginUser().getUser();

        if(user == null || user.getAccountType() != LoginConstants.USER_TYPE_WX){
            return null;
        }

        //获取当前默认绑定设备
        YxUserAndDevice query = new YxUserAndDevice();
        query.setUserId(user.getUserId());
        query.setIsDefult(DeviceConstants.USER_DEFULT_DEVICE);
        List<YxUserAndDevice> yxUserAndDeviceList= yxUserAndDeviceService.selectYxUserAndDeviceList(query);
        if(yxUserAndDeviceList == null || yxUserAndDeviceList.size() != 1){
            return null;
        }

        YxDeviceNoticeMessage yxDeviceNoticeMessage = new YxDeviceNoticeMessage();
        yxDeviceNoticeMessage.setDeviceId(yxUserAndDeviceList.get(0).getDeviceId());

        PageUtils.startPage();
        List<YxDeviceNoticeMessageVo> list = yxDeviceNoticeMessageService.selectYxDeviceNoticeMessageVoList(yxDeviceNoticeMessage);

        return list;
    }
}
